//朱浩榕
//首页添加登陆失败后的日志
function errorhomelog(){
    $.ajax({
        type:'post',
        url:'/land/erroraddlog',
        data:{errorusername,errorpassword,zhuangtai,ip},
        dataType:'json',
        success:function(data){

        }
    })
}
//朱浩榕
//首页添加登陆成功后的日志
function successhomelog(){
    $.ajax({
        type:'post',
        url:'/land/successaddlog',
        data:{errorusername,errorpassword,zhuangtai,ip},
        dataType:'json',
        success:function(data){

        }
    })
}

function checkPassword() {
    var pass = document.getElementById("password");
    var regs = /^[a-zA-Z0-9_]{6,12}$/;
    var message = document.getElementById("message2")
    if(regs.test(pass.value)) {
        message.style.color = "greenyellow";
        message.innerHTML = "✔";
        return true;
    } else {
        message.style.color = "red";
        message.innerHTML = "❌";
        return false;
    }
}
var regg;
function checkPhone() {
    var phone = document.getElementById("userphone");
    regg = /^[1][3,4,5,7,8][0-9]{9}$/;
    var message = document.getElementById("message4")
    if(regg.test(phone.value)) {
        message.style.color = "greenyellow";
        message.innerHTML = "✔";
        return true;
    } else {
        message.style.color = "red";
        message.innerHTML = "❌";
        return false;
    }
}
function login(username,password){
    $.ajax({
        url:"/login1",
        type:"POST",
        dataType:"json",
        data:{
            username:username,
            password:password
        },
        success: function (res) {
            console.log(res)
            if(res.state == '200'){
                if(res.data.sealState === 0){
                    layer.msg("账号封禁中",{time:1000,icon:5},function(){
                        console.log(res)
                        errorusername = username;
                        errorpassword = password;
                        zhuangtai = "登陆失败";
                        errorhomelog();
                        window.location = '../../login.html';
                    });
                }else {
                    layer.msg("登录成功", {time: 500, icon: 6}, function () {
                        console.log(res)
                        sessionStorage.setItem("userA", JSON.stringify(res.data));
                        localStorage.setItem("uid", res.data.userId);
                        localStorage.setItem("uname", res.data.userName);
                        localStorage.setItem("userPhone", res.data.userPhone);
                        localStorage.setItem("userRemark",res.data.userRemark);
                        localStorage.setItem("userEmail",res.data.userEmail);
                        localStorage.setItem("userDateBirth",res.data.userDateBirth);


                        localStorage.setItem("userpwd", res.data.userPwd);
                        // console.log(sessionStorage.getItem("userA"))
                        console.log(localStorage.getItem("uname"));
                        console.log(localStorage.getItem("userremark"));


                        errorusername = username;
                        errorpassword = password;
                        zhuangtai = "登陆成功";
                        successhomelog();
                        window.location.href = '/index';
                    });
                }
            }else if(res.state >= '300'){
                console.log(res)

                    layer.msg("账号密码不正确",{time:1000,icon:5},function(){
                        console.log(res)
                        errorusername = username;
                        errorpassword = password;
                        zhuangtai = "登陆失败";
                        errorhomelog();
                        // window.location = '../../login.html';
                    });
            }
        }
    })
}

$("#yello").on("click",function(){
    location.href="/register/htl";    //跳转注册页面
})
// function checkUser() {
//     var user = document.getElementById("username");
//     var regs = /^[a-zA-Z0-9\u4e00-\u9fa5_]{5,18}$/;
//     var message = document.getElementById("message1")
//     if(regs.test(user.value)) {
//         message.style.color = "greenyellow";
//         message.innerHTML = "✔";
//         return true;
//
//     } else {
//         message.style.color = "red";
//         message.innerHTML = "请输入6-18位中英文用户名！";
//         return false;
//     }
// }

function getkaptcha(){
    $("#captchaPic").attr('src','/kaptcha?d='+ new Date());
}

function getzym(){
    const inputBox = document.getElementById("userphone");
    /**
     * 获取input输入框中的值
     */
    let inputVal = inputBox.value;
    if(inputVal == ''){
        layer.msg('请输入手机号');
        return false;
    }
    if(!regg.test(inputVal)){
        layer.msg('手机号格式错误');
        return false;
    }
    $.ajax({
        type:'post',
        url:'/yzm',
        data:{"userphone":inputVal},
        dataType:'json',
        success:function(res){
            if(res.state == '200'){
                layer.msg(res.msg,{time:500,icon:6});
            }else{
                layer.msg(res.msg,{time:1000,icon:5});
            }
        }
    })
}



// 登陆日志声明的变量  home中的登陆日志（朱浩榕）
var errorusername ;
var errorpassword;
var zhuangtai;
var ip;

layui.use(['form'], function () {
    var form = layui.form,
        layer = layui.layer;

    /**
     * 登录过期的时候，跳出ifram框架
     */
    if (top.location != self.location) top.location = self.location;

    // 进行登录操作
    // form.on('submit(login)', function (data) {
    //     data = data.field;
    //     // if(document.getElementById("xyon").checked == false) {
    //     //     layer.msg('请勾选协议');
    //     //     return false;
    //     // }
    //     if (data.userName == '') {
    //         layer.msg('用户名不能为空');
    //         return false;
    //     }
    //
    //     if (data.userPwd == '') {
    //         layer.msg('密码不能为空');
    //         return false;
    //     }
    //     if (data.captcha == '') {
    //         layer.msg('要填写验证码');
    //         return false;
    //     }
    //     $.ajax({
    //         type:"POST",
    //         url:'/login1',
    //         data:data,
    //         dataType:'json',
    //         success:function(res){
    //             console.log(res)
    //             if(res.state == '200'){
    //                 layer.msg(res.msg,{time:500,icon:6},function (){
    //                     sessionStorage.setItem("userA", JSON.stringify(res.data));
    //                     localStorage.setItem("uid",res.data.userId);
    //                     localStorage.setItem("uname",res.data.userName);
    //                     window.location.href = '/indixi';
    //                 });
    //             }else if(res.state >= '300'){
    //                 layer.msg(res.msg,{time:1000,icon:5},function(){
    //                     window.location = '../../login.html';
    //                 });
    //             }
    //         }
    //     })
    // });


    $("#login").click(function (data) {


        data = data.field;


        var username=$("#username").val();
        var password=$("#password").val();


        console.log(username);
        //拿到前端传入的用户名和密码
        console.log("当前登陆的用户名字:  " + username  );
        console.log("当前登陆的用户密码:  " + password );

        // var jzw=$("#jzw").val();
            login(username,password);

    })


















    form.on('submit(login2)', function (data) {

        data = data.field;
        // if(document.getElementById("xytw").checked == false) {
        //     layer.msg('请勾选协议');
        //     return false;
        // }
        if (data.userPhone == '') {
            layer.msg('手机号不能为空');
            return false;
        }
        if (data.yzm == '') {
            layer.msg('验证码不能为空');
            return false;
        }
        if(!regg.test(data.userPhone)){
            layer.msg('手机号格式错误');
            return false;
        }
        $.ajax({
            type:'post',
            url:'/login2',
            data:data,
            dataType:'json',
            success:function(res){
                if(res.state == '200'){

                    layer.msg(res.msg,{time:500,icon:6},function (){
                        console.log(res.data);
                        sessionStorage.setItem("userA", JSON.stringify(res.data));
                        localStorage.setItem("uid",res.data.userId);
                        localStorage.setItem("uname",res.data.userName);
                        login(res.data.userName,res.data.userPwd);
                    });
                }else{
                    layer.msg(res.msg,{time:1000,icon:5});
                }
            }
        })
        return false;
    });
});
